<template>
    <view class="page-my-index">
        <view class="fly-card info">
            <view class="item">
                <view class="thumb" @click="gotoPage('/packageA/account/info')">
                    <u-image width="120rpx" height="120rpx" :src="userInfo?.avatar ? proxy.$util.getFullImage(userInfo?.avatar) : proxy.$icon.avatar" shape="circle"></u-image>
                </view>
                <view class="content">
                    <templat v-if="isLogin">
                        <view class="flex-between">
                            <view class="title" @click="gotoPage('/packageA/account/info')">{{ userInfo?.nickname || userInfo?.username}} <u-icon name="edit-pen" size="32rpx"></u-icon></view>
                            <u-tag class="tag-cyan" shape="circle" :text="'信用分：' + userInfo?.creditScore" size="mini" @click.stop="refShowAgreement.init('creditScore')"></u-tag>
                        </view>
                        <view class="desc mt-1 ellipsis-2" @click="gotoPage('/packageA/account/info')" v-if="v">
                            {{ userInfo?.intro || '点击这里，填写简介' }}
                        </view>
                    </templat>
                    <view v-else @click="proxy.$util.gotoPage('/pages/login/index')" style="font-size:34rpx;line-height:120rpx;">请先登录</view>
                </view>
                
            </view>
            <view class="banner">
                <u-button class="btn-share" open-type="share"></u-button>
                <u-image width="100%" mode="widthFix" :src="proxy.$icon.invite"></u-image>
            </view>
        </view>    
        <view class="mx-4 py-4">  
            <view class="nav">
                <u-row gutter="18">
                    <u-col span="6" v-for="(item,index) in nav" :key="index">
                        <view class="flex-between" @click="gotoPage(item.url)">
                            <view class="flex-block">
                                <u-image width="48rpx" height="48rpx" :src="item.icon"></u-image>
                                <text class="title">{{ item.title }}</text>
                            </view>
                            <u-icon name="arrow-right" color="#969799"></u-icon>
                        </view>
                    </u-col>
                </u-row>
            </view>
            <view class="menu">
                <u-cell-group :border="false">
                    <u-cell-item v-if="v" title="我的会员" :icon="proxy.$icon.mymember" @click="gotoPage('/packageA/member/index')"/>
                    <template v-if="role.isMember">
                        <u-cell-item v-for="(item,index) in menu.member" :key="index" :title="item.title" :icon="item.icon" @click="gotoPage(item.url)"/>
                    </template>
                    <!-- <template v-if="role.isSub">
                        <u-cell-item v-for="(item,index) in menuSub" :key="index" :title="item.title" :icon="item.icon" :border-bottom="menu.length - 1 == index ? false : true" @click="gotoPage(item.url)"/>
                    </template>
                    <template v-if="role.isMaster">
                        <u-cell-item v-for="(item,index) in menuMaster" :key="index" :title="item.title" :icon="item.icon" :border-bottom="menu.length - 1 == index ? false : true" @click="gotoPage(item.url)"/>
                    </template> -->
                    <template v-else>
                        <u-cell-item v-for="(item,index) in menu.branch" :key="index" :title="item.title" :icon="item.icon" :border-bottom="menu.length - 1 == index ? false : true" @click="gotoPage(item.url)"/>
                    </template>
                </u-cell-group>
            </view>
            <view class="menu mt-2">
                <u-cell-group>
                    <u-cell-item title="设置" :icon="proxy.$icon.setting" :value="'V'+currentVersion" :border-bottom="false" @click="gotoPage('/packageA/setting/index')"/>
                </u-cell-group>
            </view>
        </view>
        <fly-tabbar ref="refFlyTabbar" current="my"/>
        <show-agreement ref="refShowAgreement" :is-close="true" :is-agree="false" />
    </view>
</template>
<script setup lang="ts">
import { onShow,onLoad,onShareAppMessage } from '@dcloudio/uni-app';
import FlyTabbar from '@/components/FlyTabbar/index.vue'
const refFlyTabbar = ref();
import showAgreement from '@/components/showAgreement/index.vue';
let refShowAgreement = ref();
import { useAppStore } from '@/stores/app';
let appStore = useAppStore();
const {proxy} = getCurrentInstance() as ComponentInternalInstance;
let userInfo = ref();
let isLogin = ref(false);
let v = ref(false); // false审核，true正式发布
let currentVersion = ref();
let role = reactive({
    isMember: false,
    isMaster: false,
    isSub: false,
});
let nav = reactive([
    {title:'订单', icon: proxy.$icon.order, url: '/packageB/bill/list'},
    {title:'活动', icon: proxy.$icon.activity, url: '/packageB/activity/list'},
    {title:'创作', icon: proxy.$icon.creation, url: '/packageB/creation/list'},
    {title:'收藏', icon: proxy.$icon.collect, url: '/packageB/creation/collect'},
    {title:'体验卡', icon: proxy.$icon.invitationcode, url: '/packageB/invitation/list'},
    {title:'客服', icon: proxy.$icon.customer, url: '/packageA/service/index'},
]);
let menu = reactive({
    member: [
        // {title:'我的会员', icon: proxy.$icon.mymember, url: '/packageA/member/index'},
        {title:'联系分会长', icon: proxy.$icon.contactbranchpresident, url: '/packageA/branch/connect'},
        {title:'建立分会', icon: proxy.$icon.establishbranch, url: '/packageA/branch/add'},
        {title:'申请转会', icon: proxy.$icon.establishbranch, url: '/packageA/branch/change'},
    ],
    branch: [],
    president: [
        {title:'我的钱包', icon: proxy.$icon.wallet, url: '/packageA/wallet/index'},
        {title:'我的分会', icon: proxy.$icon.mybranch, url: '/packageA/branch/list'},
    ],
    // master: [
    //     {title:'我的分会', icon: proxy.$icon.mybranch, url: '/packageA/branch/list'},
    // ],
    sub: [
        {title:'活动管理', icon: proxy.$icon.eventmanagement, url: '/packageB/activity/examine'},
        {title:'转会审批', icon: proxy.$icon.membermanagement, url: '/packageA/branch/changeAudit'},
        // {title:'分会成员', icon: proxy.$icon.membermanagement, url: '/packageA/branch/member'},
    ]
});

const gotoPage = (url) => {
    proxy.$util.gotoPage(url)
}
onShareAppMessage((option) =>{
    let obj  = {
        title: '邀请你来加入方圆汇',
		path: 'pages/register/index',
		imageUrl: proxy.$icon.banner
    };
    if(isLogin.value){
        obj.title = userInfo.value.nickname + obj.title;
        obj.path = obj.path + '?inviterUserId=' + userInfo.value.userId;
    }
    return obj;
	// return {
	// 	title: (userInfo.value.nickname ? userInfo.value.nickname : userInfo.value.username) + '邀请你来加入读书会',
	// 	path: 'pages/register/index?inviterUserId='+ userInfo.value.userId,
	// 	imageUrl: proxy.$icon.banner
	// }
});
onShow(() => {
    currentVersion.value = proxy.$util.version;
    v.value = appStore.versionStatus;
    if(proxy.$util.checkLogin('nologin')){
        isLogin.value = true;
        appStore.getUserInfo().then(res => {
            userInfo.value = res;
        });
        appStore.getUserRole().then(res => {
            menu.branch = JSON.parse(JSON.stringify(menu.president));
            role.isMaster = res.indexOf(1) != -1;
            role.isSub = res.indexOf(2) != -1;
            role.isMember = res.indexOf(3) != -1;
            if(role.isSub){
                menu.branch = menu.branch.concat(menu.sub);
                if(role.isMaster){
                    menu.branch[2].url = menu.branch[2].url + '?type=isMaster'
                };
            };
            if(role.isMaster){
                menu.branch[1].url = menu.branch[1].url + '?type=isMaster'
            };
            if(role.isMember){
                appStore.getPTUserGuilds();
            }
        });
    }else{
        isLogin.value = false;
    }
    
 });
</script>